﻿using System;
using System.Web;
using FBGraph.Configuration;
using FBGraph.Web.Configuration;

namespace FBGraph.Web.Security
{
    /// <summary>Provides basic functionality for handlers that providing authentication and authorization-related functionality for WebForms applications.</summary>
    public abstract class FBGraphHttpHandler : IHttpHandler
    {
        Boolean IHttpHandler.IsReusable
        {
            get { return false; }
        }

        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            this.HttpContext = new HttpContextWrapper(context);
            this.FbContext = FBGraphHttpContext.Current(this.HttpContext);
            this.Config = new FBGraphConfigurationManager();
            this.AuthUrl = VirtualPathUtility.MakeRelative(String.Empty, this.Config.GetAuthUrl());

            this.ProcessRequest();
        }

        /// <summary>In derived classes, performs the processing of the HTTP request.</summary>
        protected abstract void ProcessRequest();

        /// <summary>Gets a reference to the current <see cref="HttpContextBase" /> object for the request.</summary>
        protected HttpContextBase HttpContext { get; private set; }

        /// <summary>Gets a reference to the current <see cref="IFBGraphHttpContext" /> implementation for the request.</summary>
        protected IFBGraphHttpContext FbContext { get; private set; }

        /// <summary>Gets a reference to the current <see cref="HttpRequestBase" /> object for the request..</summary>
        protected HttpRequestBase Request { get { return this.HttpContext.Request; } }

        /// <summary>Gets a reference to the current <see cref="HttpResponseBase" /> object for the request.</summary>
        protected HttpResponseBase Response { get { return this.HttpContext.Response; } }

        /// <summary>Gets a reference to a <see cref="FBGraphConfigurationManager" />, which provides access to FBGraph.NET configuration settings.</summary>
        protected FBGraphConfigurationManager Config { get; private set; }

        /// <summary>Gets the url that the user should be redirected to for authorization.</summary>
        protected String AuthUrl { get; private set; }

        /// <summary>Gets the return url if it is specified on the querystring.</summary>
        protected String ReturnUrl
        {
            get { return this.Request.QueryString["returnUrl"]; }
        }
    }
}